#!/usr/bin/perl -w
use DBI;
use mydb;
use myvideo;
use Config::IniFiles;
use threads;
use runtran;

	my $cfg = new Config::IniFiles( -file=>"server.ini",
                                 -allowcontinue=>0,
                                 -reloadwarn=>1,
                                -nocase=>1,);


	my $servername = $cfg->val('register','servername');
	my $type = $cfg->val('register','type');

	if(
	   $servername eq'undef' || $servername eq ''||
	   $type eq'undef' || $type eq ''

	) {

		die("取配置文件错误！");
	}

	if(!($type eq 'merage')) {

		die "服务器类型错误！";
	}

	my_connect();

	$sql = "SELECT * FROM c_server WHERE status = 'normal' and sclass = \'$type\' and server = \'$servername\'";
	$sth = $dbconn->prepare($sql) or die $dbconn->errstr;
	$sth->execute() or die "错误: " . $sth->errstr;

#判断当前的服务器的状态是否是正常
	my @dat=$sth->fetchrow_array();
	my $rows = $sth->rows;

	if($rows != 1) {

	   die("[error] the server($servername) isn't active!\n");
	} 

	$sql = "update c_control set status = \'merage_wait_$servername\', over = 0, running = 0 where tid  = (select tid from c_control where status = 'transcode_down' and total = over order by tid limit 0,1)";
	$sth = $dbconn->prepare($sql) or die $dbconn->errstr;
	$sth->execute() or die "错误:$dbconn->errstr";

	$sql = "select * from c_control where status = \'merage_wait_$servername\'";
	$sth = $dbconn->prepare($sql) or die $dbconn->errstr;
	$sth->execute() or die "错误:$dbconn->errstr";

	while(@d_ary = $sth->fetchrow_array()) {

	#对于任务控制表设定开始的进程数

		$sql = "update c_control set running = running + 1, status=\'merage_scp_$servername\' WHERE tid = $d_ary[0]";
                $sth1 = $dbconn->prepare($sql) or die $dbconn->errstr;
                $sth1->execute() or die "错误:$sth->errstr";

		$sql = "select * from t_prepare where tid = $d_ary[0]";
                $sth1 = $dbconn->prepare($sql) or die $dbconn->errstr;
                $sth1->execute() or die "错误:$sth->errstr";

		while(@d_ary000 = $sth1->fetchrow_array()) {

			#生成处理业务的线程
			$pid = $d_ary[0];
			$tid = $d_ary[1];

 			my $fpid = fork() ;

  			if ( $fpid == 0 ){

				meragescp("$servername", $pid, $tid);
    				exit;
  			} else {
	
    				#print "this is parent \n";

  			}
		}
	
	}

